
module PNEngine
  class ExecutionContext < ActiveRecord::Base
      include Resource::InstanceMethods
      extend Resource::ClassMethods

      has_many :transition_modes, :as => :execution_container
      belongs_to :case

    def has_transition?(transition)
      transition_modes.collect{|m| m.transition}.include?(transition)
    end
    
    def has_transition_and_input(transition, input_tokens)
      return (has_transition?(transition) && get_modes_with_equal_input(transition, input_tokens))
    end
    
    private
    
    def get_modes_with_equal_input(transition, input_tokens)
      modes = transition_modes.select{|mode| mode.transition == transition}
      curr_value_arr = input_tokens.collect{|t| t.values}
      found_modes = modes.select! do |mode|
        old_value_arr = mode.input_tokens.collect {|t| t.values}
        # if any old value isn't equal to current value(include?(false) returns true), don't select mode
        !old_value_arr.collect{|values| curr_value_arr.include?(values)}.include?(false)
      end
      return found_modes
    end
  end
end